<template>
  <div>
    <el-card :bordered="false" shadow="never" class="ivu-mt">
      <div class="box" ref="picBox">
        <div class="Modal">
          <div class="colLeft">
            <div class="Nav">
              <div class="trees-coadd">
                <div class="scollhide">
                  <div :class="isPage ? 'tree' : 'isTree'">
                    <el-tree
                        :data="treeData"
                        node-key="id"
                        default-expand-all
                        highlight-current
                        :expand-on-click-node="false"
                        @node-click="appendBtn"
                        :current-node-key="treeId"
                    >
                <span class="custom-tree-node" slot-scope="{ data }">
                  <span class="file-name">
                    <img class="icon" src="@/assets/images/file.jpg" />
                    <span class="name line1">{{ data.title }}</span>
                  </span>
                </span>
                    </el-tree>
                  </div>
                </div>
              </div>
            </div>
            <div class="conter">
              <div class="algorithmList acea-row" :class="{ 'is-modal': !isPage }">
                <div style="width: 100%">
                  <div v-show="isShowAlgorithm" class="imagesNo">
                    <i class="el-icon-picture" style="color: #dbdbdb; font-size: 60px"></i>
                    <span class="imagesNo_sp">算法演示模块为空</span>
                  </div>
                  <div ref="imgListBox" class="acea-row mb10">
                    <div
                        class="algorithmList_pic mb10 mt10"
                        v-for="(item, index) in algorithmList"
                        :key="index"
                        :style="{ margin: picmargin }"
                    >
                      <p class="number" v-if="item.num > 0">
                        <el-badge :value="item.num" type="primary">
                          <a href="#" class="demo-badge"></a>
                        </el-badge>
                      </p>
                      <div class="img" :class="item.isSelect ? 'on' : ''">
                        <img v-lazy="item.image" @click.stop="changImage(item, index)" />
                      </div>

                      <div class="operate-item">
                        <p v-if="!item.isEdit">
                          {{ item.name }}
                        </p>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="footer acea-row row-right">
                <pagination
                    v-if="total"
                    :total="total"
                    :pageCount="9"
                    layout="total, prev, pager, next"
                    :page.sync="fileData.page"
                    @pagination="pageChange"
                    :limit.sync="fileData.limit"
                ></pagination>
              </div>
            </div>
          </div>
          <div class="images" v-show="false" v-viewer="{ movable: false }">
            <img v-for="src in algorithmList" :src="src.att_dir" :key="src.att_id" />
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import { VueTreeList, Tree, TreeNode } from 'vue-tree-list';
import { algorithmListApi, algorithmCategoryListApi } from '@/api/algorithm';
export default {
  components: { VueTreeList },
  name: 'system_file',
  data() {
    return {
      pageLimit: 12,
      uploadShow: false,
      isPage: true,
      treeData: [],
      treeData2: [],
      treeId: '',
      isIframe: true,
      algorithmList: [],
      fileData: {
        pid: 0,
        real_name: '',
        page: 1,
        limit: this.pageLimit || 18,
      },
      loading: false,
      total: 0,
      picmargin: '5px', //默认距离右边距离
      isShowAlgorithm: false,
    };
  },
  mounted() {
    this.uploadShow = true;
    this.getCateList();
    this.getAlgorithmList();
  },
  methods: {
    radioChange() {
      this.initData();
    },
    lookImg(item) {
      this.imageUrl = item.att_dir;
      const viewer = this.$el.querySelector('.images').$viewer;
      viewer.show();
      this.$nextTick(() => {
        let i = this.algorithmList.findIndex((e) => e.att_dir === item.att_dir);
        viewer.update().view(i);
      });
    },
    onChangeName(params) {
      if (params.eventType == 'blur') {
        let data = {
          name: params.newName,
          id: params.id,
        };
        interfaceEditName(data)
            .then((res) => {
              this.$message.success(res.msg);
            })
            .catch((err) => {
              this.$message.error(err);
            });
      }
    },
    // 添加分类
    addSort() {
      this.append({ id: this.treeId || 0 });
    },
    // 点击菜单
    clickMenu(data, name) {
      if (name == 1) {
        this.append(data);
      }
    },
    // 上传头部token
    getToken() {
      this.header['Authori-zation'] = 'Bearer ' + getCookies('token');
    },
    initData() {
      this.checkPicList = [];
      this.ids = [];
      this.multipleSelection = [];
    },
    // 鼠标移入 移出
    onMouseOver(root, node, data) {
      event.preventDefault();
      data.flag = !data.flag;
      if (data.flag2) {
        data.flag2 = false;
      }
    },
    // 点击树
    appendBtn(data) {
      this.treeId = data.id;
      this.fileData.page = 1;
      this.getAlgorithmList();
    },
    // 点击添加
    append(data) {
      this.treeId = data.id;
    },
    // 搜索分类
    changePage() {
      this.getCateList();
    },
    // 分类列表树
    getCateList(type) {
      let data = {
        title: '全部',
        id: '',
        pid: 0,
      };
      algorithmCategoryListApi()
          .then(async (res) => {
            if (type !== 'search') {
              this.treeData2 = JSON.parse(JSON.stringify([...res.data.list]));
            }
            res.data.list.unshift(data);
            this.treeData = res.data.list;
          })
          .catch((res) => {
            this.$message.error(res.msg);
          });
    },
    loadData(item, callback) {
      getCategoryListApi({
        pid: item.id,
      })
          .then(async (res) => {
            const data = res.data.list;
            callback(data);
          })
          .catch((res) => {});
    },
    // 新建分类
    add() {
      this.treeId = 0;
      this.getFrom();
    },
    // 文件列表
    getAlgorithmList() {
      let params = {
        category_id: this.treeId
      }
      algorithmListApi(params)
          .then(async (res) => {
            res.data.list.forEach((el) => {
              el.isSelect = false;
              el.isEdit = false;
              el.isShowEdit = false;
              el.realName = false;
              el.num = 0;
            });
            this.algorithmList = res.data.list;

            if (this.algorithmList.length) {
              this.isShowAlgorithm = false;
            } else {
              this.isShowAlgorithm = true;
            }
            this.total = res.data.count;
            this.$nextTick(() => {
              //确保dom加载完毕
              // this.showSelectData();
            });
          })
          .catch((res) => {
            this.$message.error(res.msg);
          });
    },
    showSelectData() {
      if (this.multipleSelection.length > 0) {
        // 判断是否存在勾选过的数据
        this.algorithmList.forEach((row) => {
          // 获取数据列表接口请求到的数据
          this.multipleSelection.forEach((item) => {
            // 勾选到的数据
            if (row.att_id === item.att_id) {
              // this.$refs.table.toggleRowSelection(item, true); // 若有重合，则回显该条数据
            }
          });
        });
      }
    },
    getRowKey(row) {
      return row.att_id;
    },
    //对象数组去重；
    unique(arr) {
      let result = arr.reduce((acc, curr) => {
        const x = acc.find((item) => item.att_id === curr.att_id);
        if (!x) {
          return acc.concat([curr]);
        } else {
          return acc;
        }
      }, []);
      return result;
    },
    //  选中某一行
    handleSelectRow(selection) {
      let arr = this.unique(selection);
      const uniqueArr = [];
      const ids = [];
      for (let i = 0; i < arr.length; i++) {
        const item = arr[i];
        if (!ids.includes(item.att_id)) {
          uniqueArr.push(item);
          ids.push(item.att_id);
        }
      }
      this.ids = ids;
      this.multipleSelection = uniqueArr;
    },
    pageChange(index) {
      this.fileData.page = index;
      this.getAlgorithmListApi();
      this.checkPicList = [];
    },
    // 选中图片
    changImage(item, index) {
      this.$router.push({ path: this.$routeProStr + '/algorithm/mall/demonstrate/' + item.id });
    },
  },
};
</script>
<style scoped lang="stylus">
.box {
  width: 100%;
  height 100%
  background: #fff;
}
::v-deep .el-card__body{
  min-height: 700px;
  padding 16px 16px 16px 0
}
::v-deep .conter .algorithmList{
  max-width 100%
}
.nameStyle {
  position: absolute;
  white-space: nowrap;
  z-index: 9;
  background: #eee;
  height: 20px;
  line-height: 20px;
  color: #555;
  border: 1px solid #ebebeb;
  padding: 0 5px;
  left: 56px;
  bottom: -18px;
}

.iconbianji1 {
  font-size: 13px;
}

.selectTreeClass {
  background: #d5e8fc;
}
.tree_tit {
  padding-top: 7px;
}
.treeBox {
  width: 100%;
  height: 100%;
  max-width: 180px;
}
.is-modal .algorithmList_pic {
  width: 100px;
  margin: 10px 5px !important;
  .img {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100px;
    height: 100px;
    background-color: rgb(248, 248, 248);
    padding: 2px;
    img {
      max-width: 96px;
      max-height: 96px;
      // object-fit: cover;
    }
    .operate-height {
      bottom: -8px;
    }
  }
}
.algorithmList_pic {
  position: relative;
  width: 146px;
  cursor: pointer;
  // margin-right: 20px !important;
  .img {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 146px;
    height: 146px;
    background-color: rgb(248, 248, 248);
    padding: 3px;
    img {
      max-width: 140px;
      max-height: 140px;
      // object-fit: cover;
    }
  }

  p {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    height: 20px;
    font-size: 12px;
    color: #515a6d;
    text-align: center;
  }

  .number {
    height: 33px;
  }

  .number {
    position: absolute;
    right: 0;
    top: 0;
  }
  ::v-deep .el-badge__content.is-fixed {
    top: 13px;
    right: 25px;
  }
}
.Nav {
  width: 100%;
  border-right: 1px solid #eee;
  min-width: 220px;
  max-width: max-content;
}
.trees-coadd {
  width: 100%;
  border-radius: 4px;
  overflow: hidden;
  position: relative;

  .scollhide {
    overflow-x: hidden;
    overflow-y: scroll;
    padding: 0px 0 10px 0;
    box-sizing: border-box;

    .isTree {
      min-height: 374px;
      max-height: 550px;
      ::v-deep .file-name {
        display: flex;
        align-items: center;
        .name {
          max-width: 7em;
        }
        .icon {
          width: 12px;
          height: 12px;
          margin-right: 8px;
        }
      }
      ::v-deep .el-tree-node {
        margin-right: 16px;
      }
      ::v-deep .el-tree-node__children .el-tree-node {
        margin-right: 0;
      }
      ::v-deep .el-tree-node__content {
        width: 100%;
        height: 36px;
      }
      ::v-deep .custom-tree-node {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-right: 20px;
        font-size: 13px;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.6);
        line-height: 13px;
      }
      ::v-deep .is-current {
        background: #f1f9ff !important;
        color: var(--prev-color-primary) !important;
      }
      ::v-deep .is-current .custom-tree-node {
        color: var(--prev-color-primary) !important;
      }
    }
  }

  .scollhide::-webkit-scrollbar {
    display: none;
  }
}

.treeSel ::v-deep .ivu-select-dropdown-list {
  padding: 0 5px !important;
  box-sizing: border-box;
  width: 200px;
}
.imagesNo {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  margin: 65px 0;

  .imagesNo_sp {
    font-size: 13px;
    color: #dbdbdb;
    line-height: 3;
  }
}

.Modal {
  width: 100%;
  height: 100%;
  background: #fff !important;
}
.fill-window {
  height: 100vh;
}
.colLeft {
  padding-right: 0 !important;
  height: 100%;
  display: flex;
  flex-wrap: nowrap;
}

.conter {
  width: 100%;
  height: 100%;
  margin-left: 20px !important;
  .iconliebiao {
    font-size: 12px;
  }
}

.conter .bnt {
  width: 100%;
  padding: 0 13px 20px 0px;
  box-sizing: border-box;
}

.conter .algorithmList {
  // width: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  min-height: 463px;
}
.conter .algorithmList.is-modal {
  max-height: 480px;
}
.right-col {
  // flex: 1;
}
.conter .algorithmList img {
  max-width: 100%;
}
.conter .algorithmList .img.on {
  border: 2px solid var(--prev-color-primary);
}

.conter .footer {
  padding: 0 20px 10px 20px;
}
.tabBox_img {
  display: flex;
  align-items: center;
}
.real-name {
  flex: 1;
}
.df-aic {
  display: flex;
  align-items: center;
}
.demo-badge {
  width: 42px;
  height: 42px;
  background: transparent;
  border-radius: 6px;
  display: inline-block;
}

.bnt ::v-deep .ivu-tree-children {
  padding: 5px 0;
}

.card-tree {
  background: #fff;
  height: 72px;
  box-sizing: border-box;
  overflow-x: scroll; /* 设置溢出滚动 */
  white-space: nowrap;
  overflow-y: hidden;
  /* 隐藏滚动条 */
  border-radius: 4px;
  scrollbar-width: none; /* firefox */
  -ms-overflow-style: none; /* IE 10+ */
}
.card-tree::-webkit-scrollbar {
  display: none; /* Chrome Safari */
}
.tabs {
  background: #fff;
  padding-top: 10px;
  border-radius: 5px 5px 0 0;
}
.operate-item {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  margin: 5px 0;
}
.operate-height {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 16px;
  position: absolute;
  bottom: -10px;
}
.operate {
  color: var(--prev-color-primary);
  font-size: 12px;
  white-space: nowrap;
}
</style>
